﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmShuidian : Form
    {
        private int updateId=-1;
        private double danjia = 0.0;
        public FrmShuidian()
        {
            InitializeComponent();
            InitializeCompanies();
            button_add.Text = "添加并关闭";
            button_add.Enabled = true;
            button_continue.Enabled = true;
            this.Text = "水电等录入-添加一条记录";
        }
        public FrmShuidian(int id)
        {
            InitializeComponent();
            InitializeCompanies();
            updateId = id;
            button_add.Text = "修改";
            button_add.Enabled = true;
            button_continue.Enabled = false;
            this.Text = "水电等录入-修改记录"+updateId;
            
        }
        public void InitializeCompanies()
        {
            List<ComboItem> companies = new List<ComboItem>();
            companies.Add(new ComboItem { Text = "", Value = "0" });
            using (DataBase db = new DataBase())
            {
                string sql = "SELECT ID,COMPANY_NAME FROM CJ_CUSTOMER_INFO order by COMPANY_NAME";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    companies.Add(new ComboItem {
                        Text = dr[1].ToString(), 
                        Value = dr[0].ToString() });

                }
            }
            comboBox_companyname.DisplayMember = "Text";
            comboBox_companyname.ValueMember = "value";
            comboBox_companyname.DataSource = companies;
        }
        private void FrmShuidian_Load(object sender, EventArgs e)
        {
            if (updateId > 0)//modify
            {
                using (DataBase db = new DataBase())
                {
                    string sql = "SELECT [FEE_TYPE],[CB_NUM],[CB_TIME],[CB_FEE],[CB_EMPLOYEE],[CB_COMMENT], "+
                                "[COMPANY_ID] FROM [CJ_MISC_FEE] WHERE [ID]="+updateId;
                    DataTable dt = db.ExecuteDataTable(sql);
                    if (dt.Rows.Count == 1)
                    {
                        comboBox_companyname.SelectedValue = dt.Rows[0][6].ToString();
                        comboBox_type.Text = dt.Rows[0][0].ToString();
                        textBox_cbnum.Text = dt.Rows[0][1].ToString();
                        DateTime cbtime = DateTime.Now;
                        DateTime.TryParse(dt.Rows[0][2].ToString(), out cbtime);
                        dateTimePicker_cbtime.Value = cbtime;
                        textBox_cbfee.Text = dt.Rows[0][3].ToString();
                        textBox_cbemployee.Text = dt.Rows[0][4].ToString();
                        textBox_comment.Text = dt.Rows[0][5].ToString();
                    }
                }
            }
            else//new
            {
                //do nothing
            }
        }

        private int AddNewRecord()
        {
            //Check integrity
            if (comboBox_companyname.Text == "" ||
                comboBox_type.Text == "")
            {
                MessageBox.Show("请指定公司名和名目!");
                return 0;
            }
            string company_id = comboBox_companyname.SelectedValue.ToString();
            string type = comboBox_type.Text;
            DateTime cb_time = dateTimePicker_cbtime.Value;
            double cb_num = 0.0;
            double.TryParse(textBox_cbnum.Text, out cb_num);
            double cb_fee = 0.0;
            double.TryParse(textBox_cbfee.Text, out cb_fee);
            string cb_employee = textBox_cbemployee.Text;
            string cb_comment = textBox_comment.Text;
            string op_name = userinfo.username;
            int op_id = userinfo.id;
            DateTime log_time = DateTime.Now;
            DateTime last_modify_date = DateTime.Now;
            string modifyComment = log_time.ToString()+":"+op_name+"创建了该条记录;";
            string sql = "INSERT INTO [CJ_MISC_FEE]([FEE_TYPE],[CB_NUM],[CB_TIME],[CB_FEE],[CB_EMPLOYEE] " +
                    ",[CB_COMMENT],[OP_NAME],[OP_ID],[LOG_TIME],[MODIFY_COMMENT],[LAST_MODUFY_DATE] " +
                    ",[COMPANY_ID]) VALUES (@FEE_TYPE,@CB_NUM,@CB_TIME,@CB_FEE,@CB_EMPLOYEE,@CB_COMMENT, " +
                    "@OP_NAME,@OP_ID,@LOG_TIME,@MODIFY_COMMENT,@LAST_MODUFY_DATE,@COMPANY_ID)";
            using (DataBase db = new DataBase())
            {
                db.AddParameter("FEE_TYPE", type);
                db.AddParameter("CB_NUM", cb_num);
                db.AddParameter("CB_TIME", cb_time);
                db.AddParameter("CB_FEE", cb_fee);
                db.AddParameter("CB_EMPLOYEE", cb_employee);
                db.AddParameter("CB_COMMENT", cb_comment);
                db.AddParameter("OP_NAME", op_name);
                db.AddParameter("OP_ID", op_id);
                db.AddParameter("LOG_TIME", log_time);
                db.AddParameter("MODIFY_COMMENT", modifyComment);
                db.AddParameter("LAST_MODUFY_DATE", last_modify_date);
                db.AddParameter("COMPANY_ID", company_id);
                return db.ExecuteNonQuery(sql);
            }
        }

        private int ModifyRecord(int id)
        {
            //Check integrity
            if (comboBox_companyname.Text == "" ||
                comboBox_type.Text == "")
            {
                MessageBox.Show("请指定公司名和名目!");
                return 0;
            }
            //get modify comment
            string modifyComment = "";
            using (DataBase db = new DataBase())
            {
                DataTable dt=db.ExecuteDataTable("select MODIFY_COMMENT from [CJ_MISC_FEE] where id=" + id);
                if (dt.Rows.Count != 1)
                {
                    MessageBox.Show("无此条记录");
                    return 0;
                }
                modifyComment = dt.Rows[0][0].ToString();
            }
            string company_id = comboBox_companyname.SelectedValue.ToString();
            string type = comboBox_type.Text;
            DateTime cb_time = dateTimePicker_cbtime.Value;
            double cb_num = 0.0;
            double.TryParse(textBox_cbnum.Text, out cb_num);
            double cb_fee = 0.0;
            double.TryParse(textBox_cbfee.Text, out cb_fee);
            string cb_employee = textBox_cbemployee.Text;
            string cb_comment = textBox_comment.Text;
            string op_name = userinfo.username;
            int op_id = userinfo.id;
            DateTime last_modify_date = DateTime.Now;
            modifyComment += last_modify_date.ToString() + ":" + op_name + "修改了该条记录;";
            string sql="UPDATE [CJ_MISC_FEE] "+
                    "   SET [FEE_TYPE] = @FEE_TYPE " +
                    "      ,[CB_NUM] = @CB_NUM " +
                    "      ,[CB_TIME] = @CB_TIME " +
                    "      ,[CB_FEE] = @CB_FEE " +
                    "      ,[CB_EMPLOYEE] = @CB_EMPLOYEE " +
                    "      ,[CB_COMMENT] = @CB_COMMENT " +
                    "      ,[OP_NAME] = @OP_NAME " +
                    "      ,[OP_ID] = @OP_ID " +
                    "      ,[MODIFY_COMMENT] = @MODIFY_COMMENT " +
                    "      ,[LAST_MODUFY_DATE] = @LAST_MODUFY_DATE " +
                    "      ,[COMPANY_ID] = @COMPANY_ID " +
                    " WHERE [ID]=" + id;
            using (DataBase db = new DataBase())
            {
                db.AddParameter("FEE_TYPE", type);
                db.AddParameter("CB_NUM", cb_num);
                db.AddParameter("CB_TIME", cb_time);
                db.AddParameter("CB_FEE", cb_fee);
                db.AddParameter("CB_EMPLOYEE", cb_employee);
                db.AddParameter("CB_COMMENT", cb_comment);
                db.AddParameter("OP_NAME", op_name);
                db.AddParameter("OP_ID", op_id);
                db.AddParameter("MODIFY_COMMENT", modifyComment);
                db.AddParameter("LAST_MODUFY_DATE", last_modify_date);
                db.AddParameter("COMPANY_ID", company_id);
                return db.ExecuteNonQuery(sql);
            }
        }

        public void ClearAll()
        {
            comboBox_companyname.SelectedValue = 0;
            comboBox_type.Text = "";
            textBox_cbemployee.Text = "";
            textBox_cbfee.Text = "";
            textBox_cbnum.Text = "";
            textBox_comment.Text = "";
            dateTimePicker_cbtime.Value = DateTime.Now;
            textBox_lastfee.Text = "";
            textBox_lastnum.Text = "";
            textBox_lasttime.Text = "";
        }
        private void button_continue_Click(object sender, EventArgs e)
        {
            if (updateId < 0)
            {
                if (AddNewRecord() == 1)
                {
                    MessageBox.Show("添加成功");
                    ClearAll();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            if (updateId > 0)
            {
                if (ModifyRecord(updateId) == 1)
                {
                    MessageBox.Show("修改成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            else
            {
                if (AddNewRecord() == 1)
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox_cbnum_TextChanged(object sender, EventArgs e)
        {
            double cbnum = 0.0;
            double.TryParse(textBox_cbnum.Text, out cbnum);
            double lastcb = 0.0;
            double.TryParse(textBox_lastnum.Text, out lastcb);
            textBox_cbfee.Text = ((cbnum-lastcb) * danjia).ToString();

            textBox_comment.Text = "抄表数:" + textBox_cbnum.Text;
        }

        private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_type.Text.Length > 0)
            {
                string sql = "select UNIT_PRICE FROM CJ_FEE_BASE WHERE UNIT_NAME='" +
                    comboBox_type.Text + "'";
                using (DataBase db = new DataBase())
                {
                    DataTable dt = db.ExecuteDataTable(sql);
                    if (dt.Rows.Count > 0)
                    {
                        double.TryParse(dt.Rows[0][0].ToString(), out danjia);
                    }
                }
                //load last record
                if (comboBox_companyname.SelectedValue == null) return;
                int company_id = int.Parse(comboBox_companyname.SelectedValue.ToString());
                if (company_id > 1)
                {
                    if (updateId == -1)
                    {
                        LoadLastRecord(company_id, comboBox_type.Text, 1);
                    }
                    else
                    {
                        LoadLastRecord(company_id, comboBox_type.Text, 2);

                    }
                }
            }
        }
        /// <summary>
        /// get last record
        /// </summary>
        /// <param name="company_id">company id</param>
        /// <param name="feetype">type</param>
        /// <param name="index">1:newest 2:second newest;1 for create, 2 for modify</param>
        private void LoadLastRecord(int company_id,string feetype,int index)
        {
            string sql = "select top "+index+
                " CB_TIME,CB_NUM,CB_FEE from cj_misc_fee "+
                "where COMPANY_ID=" + company_id + " and FEE_TYPE='"+
                feetype+"' order by CB_TIME DESC";
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                if (dt.Rows.Count == index)
                {
                    textBox_lastfee.Text = dt.Rows[index - 1][2].ToString();
                    textBox_lastnum.Text = dt.Rows[index - 1][1].ToString();
                    textBox_lasttime.Text = dt.Rows[index - 1][0].ToString();
                }
            }
        }

        private void comboBox_companyname_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_companyname.SelectedValue == null) return;
            int company_id = int.Parse(comboBox_companyname.SelectedValue.ToString());
            if (comboBox_type.Text.Length >0 && company_id>0)
            {
                if (updateId == -1)
                {
                    LoadLastRecord(company_id, comboBox_type.Text, 1);
                }
                else
                {
                    LoadLastRecord(company_id, comboBox_type.Text, 2);

                }
            }
        }
    }
}
